/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.consultorio.controle;

import br.com.consultorio.bean.Funcao;
import br.com.consultorio.dao.FuncaoDao;
import br.com.consultorio.dao.FuncaoDaoImp;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author L3nfers
 */
@WebServlet(name = "ServletFuncao", urlPatterns = {"/ServletFuncao"})
public class ServletFuncao extends HttpServlet {

// ----------------------------------------------------------------------------- 
    private HttpServletRequest request;
    private HttpServletResponse response;
    private RequestDispatcher rd;
    private Funcao cargo;
    FuncaoDao cargoDao;

    protected void processRequest() throws ServletException, IOException {
        String cmd = request.getParameter("cmd");


        try {
            switch (cmd) {
                case "salvar":
                    salvar();
                    break;
                case "alterar":
                    alterar();
                    break;
                case "excluir":
                    excluir();
                    break;
                case "listar":
                    listar();
                    break;
                case "pFuncao":
                    pesquisaFuncao();
                    break;
            }
            rd.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ServletFuncao.class.getName()).
                    log(Level.SEVERE, null, ex);
        }
    }
// -----------------------------------------------------------------------------

    private void salvar() {
        cargoDao = new FuncaoDaoImp();

        try {
            cargoDao.salvar(carregaFuncao());
            request.setAttribute("msg", "Funcao salvo com sucesso");
            rd = request.getRequestDispatcher("index.jsp");
        } catch (Exception ex) {
            request.setAttribute("msg", "Erro ao salvar Funcao " + ex);
            rd = request.getRequestDispatcher("cadFuncao.jsp");
            Logger.getLogger(ServletFuncao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
// -----------------------------------------------------------------------------

    private void alterar() {
        cargoDao = new FuncaoDaoImp();

        try {
            cargoDao.alterar(carregaFuncao());
            request.setAttribute("msg", "Funcao alterado com sucesso");
            rd = request.getRequestDispatcher("index.jsp");
        } catch (Exception ex) {
            request.setAttribute("msg", "Erro ao alterar Funcao " + ex);
            Logger.getLogger(ServletFuncao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
// -----------------------------------------------------------------------------

    private void excluir() {
        cargoDao = new FuncaoDaoImp();

        String idCmd = request.getParameter("id");
        int id = Integer.parseInt(idCmd);
        try {
            cargoDao.excluir(id);
            request.setAttribute("msg", "Funcao excluído com sucesso");
            rd = request.getRequestDispatcher("index.jsp");
        } catch (Exception ex) {
            request.setAttribute("msg", "Erro ao excluir Funcao " + ex);
            Logger.getLogger(ServletFuncao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
// -----------------------------------------------------------------------------

    private void listar() {
        cargoDao = new FuncaoDaoImp();

        try {
            List cargos = cargoDao.listar();
            request.setAttribute("cargos", cargos);
            rd = request.getRequestDispatcher("mostraFuncao.jsp");
        } catch (Exception ex) {
            request.setAttribute("msg", "Erro ao listar Funcaos " + ex);
            Logger.getLogger(ServletFuncao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
// -----------------------------------------------------------------------------

    private Funcao carregaFuncao() {
        cargo = new Funcao();
        String id = request.getParameter("id");
        if (id != null) {
            cargo.setId(Integer.parseInt(id));
        }
        cargo.setNome(request.getParameter("nome"));
        cargo.setDescricao(request.getParameter("descricao"));

        return cargo;
    }
// -----------------------------------------------------------------------------    

    private void pesquisaFuncao() {
        cargoDao = new FuncaoDaoImp();

        String idForm = request.getParameter("id");
        int id = Integer.parseInt(idForm);
        try {
            cargo = (Funcao) cargoDao.procurar(id);
            request.setAttribute("cargo", cargo);
            rd = request.getRequestDispatcher("formAtuFuncao.jsp");
        } catch (Exception ex) {
            Logger.getLogger(ServletFuncao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
// -----------------------------------------------------------------------------

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.request = request;
        this.response = response;
        processRequest();
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}